<template>
	<view>
		<zy-table :tableWidth="'560px'" :headList="headList" :dataList="dataList" :operationShow="true"
			:operationList="operationList">
		</zy-table>
		<!-- 底部按钮 -->
		<bottomBtn :leftText="$t('homepage.AccessoryManagement.goBack')"
			:rightText="$t('homepage.AccessoryManagement.outStorage')" @rightBtn="submitBtn" @leftBtn="backBtn">
		</bottomBtn>
		<!-- 编辑弹出层 -->
		<u-modal :show="tableShow" :title="$t('homepage.AccessoryManagement.outStorageNumSure')" showCancelButton
			@confirm="tableConfirm" @cancel="tableCancel">
			<zy-table :scrollHeight="50" :headList="headModelList" :dataList="dataModelList" :checkboxShow="true"
				@handleSelectionChange="handleSelectionChange">
			</zy-table>
		</u-modal>
		<!-- 出库弹出层 -->
		<u-modal :show="show" :title="$t('homepage.AccessoryManagement.partOutStorageSure')"
			:content="$t('homepage.AccessoryManagement.isSureOutstorage')" showCancelButton @confirm="confirm"
			@cancel="cancel">
		</u-modal>
	</view>
</template>

<script>
	import {
		getMapyByField,
		findListByForeignId,
		applyDetailQueryListMap,
		saveWarehouseOut
	} from "@/api/homepage.js"
	import bottomBtn from "@/components/bottomBtn/index.vue"
	export default {
		components: {
			bottomBtn
		},
		data() {
			return {
				headList: [{
						name: this.$t('public.code'),
						key: 'code',
					},
					{
						name: this.$t('public.name'),
						key: 'name',
					},
					{
						name: this.$t('public.spec'),
						key: 'spec',
					},
					{
						name: this.$t('public.num'),
						key: 'num',
					},
					{
						name: this.$t('public.deliveryNum'),
						key: 'deliveryNum',
					},
				],
				dataList: [],
				operationList: [{
					kind: "button",
					type: "primary",
					handle: this.editBtn,
					label: this.$t('public.edit'),
				}],
				headModelList: [{
						name: this.$t('public.spec'),
						key: 'spec',
					},
					{
						name: this.$t('public.num'),
						key: 'surplusNum',
					}
				],
				dataModelList: [],
				id: '',
				tableShow: false, //编辑弹出层
				chooseForeignId: '',
				chooseNums: [], //编辑弹出层选中出库数量
				chooseIds: [], //编辑弹出层选中出库id
				show: false,
				warehouseInId: '',
				dataNum: '',
			}
		},
		onLoad(option) {
			this.id = option.scanDeliveryId
			this.GetApplyDetailQueryListMap()
		},
		methods: {
			GetApplyDetailQueryListMap() {
				applyDetailQueryListMap({
					warehouseOutId: this.id,
				}).then((res) => {
					if (res.code == 0) {
						this.dataList = res.data.map(m => {
							return Object.assign(m, {
								deliveryNum: Number(0)
							})
						})
					}
				})
			},
			// 表格编辑
			editBtn(item, index) {
				this.dataModelList = []
				this.chooseForeignId = item.foreignId
				this.dataNum = item.num
				findListByForeignId({
					foreignId: this.chooseForeignId,
				}).then((res) => {
					if (res.code == 0) {
						this.dataModelList = res.data
						this.tableShow = true
					} else {
						uni.$u.toast(res.message)
					}
				})
			},
			// 点击多选框
			handleSelectionChange(e) {
				this.chooseNums = []
				this.chooseIds = []
				for (var i = 0; i < e.value.length; i++) {
					this.chooseNums.push(e.value[i].surplusNum)
					this.chooseIds.push(e.value[i].warehouseInId)
				}
			},
			tableCancel() {
				this.tableShow = false
			},
			tableConfirm() {
				var sum = 0;
				for (var i = 0; i < this.chooseNums.length; i++) {
					sum = sum + this.chooseNums[i];
				}
				for (var j = 0; j < this.dataList.length; j++) {
					this.$set(this.dataList[j], "warehouseInId", this.warehouseInId)
					if (this.chooseForeignId == this.dataList[j].foreignId) {
						this.$set(this.dataList[j], "deliveryNum", "0")
						this.$set(this.dataList[j], "chooseIds", this.chooseIds)
						if (this.dataNum < sum) {
							uni.$u.toast(this.$t('homepage.AccessoryManagement.outStorageErrorTip'))
						} else {
							this.$set(this.dataList[j], "deliveryNum", Number(sum))
						}
					}
					this.tableShow = false
				}
			},
			// 返回
			backBtn() {
				uni.navigateBack();
			},
			// 提交
			submitBtn() {
				this.show = true
			},
			cancel() {
				this.show = false
			},
			confirm() {
				const detailDatas = [];
				for (var i = 0; i < this.dataList.length; i++) {
					if (this.dataList[i].deliveryNum != 0) {
						const temp = {
							foreignId: this.dataList[i].foreignId,
							warehouseOutId: this.id,
							warehousePositionId: this.dataList[i].warehousePositionId,
							warehouseStorageId: this.dataList[i].warehouseStorageId,
							warehouseInId: this.dataList[i].chooseIds.toString(),
							immediateWarehouseId: this.dataList[i].immediateWarehouseId,
							num: this.dataList[i].deliveryNum,
						}
						detailDatas.push(temp);
					}
				};
				saveWarehouseOut({
					id: this.id,
					dataArray: JSON.stringify(detailDatas),
				}).then((res) => {
					this.show = false
					uni.$u.toast(res.message)
					this.backBtn()
				}).catch((res) => {
					uni.$u.toast(res.message)
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
</style>